<template>
  <el-container class="page-container">
    <el-header class="page-header">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="用户名">
          <el-input v-model="searchForm.username" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="searchForm.phoneNumber" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item class="form-actions">
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-header>

    <el-main class="page-main">
      <div class="action-bar">
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </div>
      <el-table :data="tableData" class="data-table" stripe>
        <el-table-column prop="username" label="用户名" width="180" />
        <el-table-column prop="avatarUrl" label="头像" width="180">
          <template #default="scope">
            <img
              :src="scope.row.avatarUrl"
              alt="用户头像"
              style="max-width: 100%; height: auto"
            />
          </template>
        </el-table-column>
        <el-table-column prop="name" label="姓名" width="180" />
        <el-table-column prop="phone" label="手机号码" width="180" />
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column prop="additionalInfo" label="备注信息" width="180" />
        <el-table-column prop="status" label="账号状态" width="180">
          <template #default="scope">
            <el-switch
              :model-value="scope.row.status === 1"
              inline-prompt
              active-text="是"
              inactive-text="否"
              @change="handleAccountStatusChange(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="200">
          <template #default="scope">
            <el-button type="primary" size="small" @click="handleEdit(scope.row)"
              >修改</el-button
            >
            <el-button type="danger" size="small" @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 30, 40]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :background="true"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-main>
    <el-dialog :title="title" v-model="dialogVisible" width="50%">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="120px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option label="管理员" value="admin"></el-option>
            <el-option label="普通用户" value="user"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="头像 URL" prop="avatarUrl">
          <el-input v-model="form.avatarUrl"></el-input>
        </el-form-item>
        <el-form-item label="备注信息" prop="additionalInfo">
          <el-input v-model="form.additionalInfo" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch v-model="form.status"></el-switch>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSubmit">提交</el-button>
          <el-button @click="handleReset">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </el-container>
</template>

<script setup>
import { onMounted, reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import request from "@/utils/request";

const tableData = ref([]);
const total = ref(0);
const currentPage = ref(1);
const pageSize = ref(10);
const dialogVisible = ref(false);
const title = ref("");

const searchForm = reactive({
  username: "",
  phoneNumber: "",
});
const form = reactive({
  id: 0,
  username: "",
  password: "",
  name: "",
  type: "",
  phone: "",
  avatarUrl: "",
  additionalInfo: "",
  status: false,
});
const rules = {
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
  name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
  type: [{ required: true, message: "请选择类型", trigger: "blur" }],
  phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
};
onMounted(() => {
  fetchData();
});

function handleCurrentChange(page) {
  currentPage.value = page;
  pageSize.value = 10;
}

async function fetchData() {
  try {
    const response = await request.get("/admin/system/user/page", {
      params: {
        current: currentPage.value,
        size: pageSize.value,
      },
    });
    if (response.data.code === 200) {
      console.log(response.data.data);
      tableData.value = response.data.data.records || [];
      total.value = response.data.data.total;
    } else {
      ElMessage.error(response.msg || "查询数据失败");
    }
  } catch (error) {
    console.error("Error fetching data:", error);
    ElMessage.error("查询数据失败");
  }
}
async function handleAccountStatusChange() {}
function handleAdd() {
  title.value = "新增用户";
  dialogVisible.value = true;
  Object.keys(form).forEach((key) => {
    form[key] = "";
  });
}
function handleReset() {
  dialogVisible.value = false;
}
</script>

<style scoped>
.page-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.page-header {
  padding: 16px;
  background: #fff;
  border-bottom: 1px solid #ebeef5;
}

.search-form {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.search-form :deep(.el-form-item) {
  margin-bottom: 0;
}

.form-actions {
  margin-left: auto;
}

.page-main {
  flex: 1;
  padding: 16px;
  background: #f5f7fa;
}

.action-bar {
  margin-bottom: 16px;
}

.data-table {
  background: #fff;
  border-radius: 4px;
  height: 650px;

  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.pagination-container {
  margin-top: 16px;
  display: flex;
  justify-content: flex-end;
}

/* 响应式适配 */
@media (max-width: 768px) {
  .search-form {
    flex-direction: column;
  }

  .form-actions {
    margin-left: 0;
    width: 100%;
    display: flex;
    justify-content: flex-end;
  }
}
</style>
